package org.pr.logoupdate.servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.pr.logoupdate.util.Constant;
import org.pr.logoupdate.util.ImageUtil;

/**
 * Servlet implementation class LogoCutServlet
 */
@WebServlet("/servlet/logoCut")
public class LogoCutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(LogoCutServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoCutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String filename = (String)request.getSession().getAttribute(Constant.UPLOAD_FILENAME);
		
		if(filename == null) {
			//no logo uploaded
			request.getSession().setAttribute(Constant.PAGE1_ERROR, "裁剪头像时似乎头像不存在");
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return;
		}
		String tempFolder = System.getProperty("java.io.tmpdir");
		String sessionId = request.getSession().getId();
		File imgFile = new File(tempFolder + File.separator + sessionId
				+ File.separator + filename);
		if (!imgFile.exists() || imgFile.isDirectory() || !imgFile.canRead() || !imgFile.canWrite()) {
			request.getSession().setAttribute(Constant.PAGE1_ERROR, "系统内部错误，请重新上传");
			response.sendRedirect(request.getContextPath() + "/index.jsp");
			return;
		}
		String imgInfo = request.getParameter("imgWidthHeight"); //w,h
		String cutInfo = request.getParameter("cutInfo");//left,top,w,h
		logger.debug("imgWidthHeight=" + imgInfo);
		logger.debug("cutInfo=" + cutInfo);
		//暂时假设传过来的参数都正确
		int imgWidth = Double.valueOf((imgInfo.split(","))[0]).intValue();
		int imgHeight = Double.valueOf((imgInfo.split(","))[1]).intValue();
		int cutLeft = Double.valueOf((cutInfo.split(","))[0]).intValue();
		int cutTop = Double.valueOf((cutInfo.split(","))[1]).intValue();
		int cutWidth = Double.valueOf((cutInfo.split(","))[2]).intValue();
		int cutHeight = Double.valueOf((cutInfo.split(","))[3]).intValue();
		ImageUtil.cutImage(imgFile.getAbsolutePath(), imgWidth, imgHeight, 
				cutLeft, cutTop, cutWidth, cutHeight);
		response.sendRedirect(request.getContextPath() + "/step3.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
